﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OL.FrameCore.Application.Person;
using OL.FrameCore.Application.Person.Dto;
using OL.FrameCore.WebUI.ViewModels.Person;

namespace OL.FrameCore.WebUI.Controllers
{
    public class PersonController : ControllerBase
    {
        IPersonService _personService;
        //ICommonDataService _commonDataService;

        public PersonController(IPersonService personService)
        {
            _personService = personService;
        }


        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult UpdatePersonInfo(UpdatePersonInfoViewRequest request)
        {
            var serviceRequest = ToMap<UpdatePersonInfoViewRequest, UpdatePersonInfoRequest>(request);
            serviceRequest.UserId = UserId;
            var result = _personService.UpdatePersonInfo(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult UpdatePassword(UpdatePasswordViewRequest request)
        {
            //RSA解密
            request.OldPassword = DecryptRSA(request.OldPassword);
            request.NewPassword = DecryptRSA(request.NewPassword);
            request.RepeatPassword = DecryptRSA(request.RepeatPassword);

            var serviceRequest = ToMap<UpdatePasswordViewRequest, UpdatePasswordRequest>(request);
            serviceRequest.UserId = UserId;
            var result = _personService.UpdatePassword(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 获取登录日志
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetLoginRecordList(GetLoginRecordListViewRequest request)
        {
            var serviceRequest = ToMap<GetLoginRecordListViewRequest, GetLoginRecordListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;
            serviceRequest.UserId = this.UserId;

            var serviceResult = _personService.GetLoginRecordList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }
    }
}
